package src.twice;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipDirectoryFrame {

    private static void zipFile(List<String> path, File targetZipFile,String base){
        try {
            FileOutputStream fos = new FileOutputStream(targetZipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);
            byte[] buffer = new byte[1024];

            for (String string : path){
                File currentFile = new File(string);
                ZipEntry entry = new ZipEntry(string.substring(base.length()+1,string.length()));
                FileInputStream fis = new FileInputStream(currentFile);
                zos.putNextEntry(entry);
                int read = 0;
                while ((read=fis.read(buffer)) != -1){
                    zos.write(buffer,0,read);
                }
                zos.closeEntry();
                fis.close();
            }
            zos.close();
            fos.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
       List<String> path = new ArrayList<>();
       path.add("C:\\Users\\22528\\Desktop\\123\\01.xlsx");
       path.add("C:\\Users\\22528\\Desktop\\123\\image.zip");
       File file = new File("C:\\Users\\22528\\Desktop\\t.zip");
       String base = "C:\\Users\\22528\\Desktop\\";
       zipFile(path,file,base);
    }
}
